/**
 * Created by Nie on 2015/12/10.
 */
app.config(['$routeProvider', '$locationProvider', function ($routeProvider, $locationProvider) {
    $routeProvider.
    when('/servant/index', {
        templateUrl: 'servant/list.html',
        controller: 'servantListController'
    }).
    when('/servant/list', {
        templateUrl: 'servant/list.html',
        controller: 'servantListController'
    }).
    when('/servant/admin/list', {
        templateUrl: 'servant/list.html',
        controller: 'servantAdminListController'
    }).
    when('/servant/edit/:servant_id', {
        templateUrl: 'servant/edit.html',
        controller: 'servantEditController'
    }).
    when('/servant/collaborate', {
        templateUrl: 'servant/collaborate.html',
        controller: 'collaborateController'
    }).
    when('/servant/talentPool', {
        templateUrl: 'servant/talentPool.html',
        controller: 'talentPoolController'
    }).
    /*证书*/
    when('/servant/cer/list', {
        templateUrl: 'servant/cer_list.html',
        controller: 'servantCerListController'
    }).
    when('/servant/cer/edit/:cer_id', {
        templateUrl: 'servant/cer_edit.html',
        controller: 'servantCerEditController'
    }).
    when('/servant/job/list', {
        templateUrl: 'servant/job_list.html',
        controller: 'servantJobListController'
    }).
    when('/servant/job/edit/:job_id', {
        templateUrl: 'servant/job_edit.html',
        controller: 'servantJobEditController'
    }).
    when('/servant/technique/list', {
        templateUrl: 'servant/technique_list.html',
        controller: 'servantTechniqueListController'
    }).
    when('/servant/technique/edit/:technique_id', {
        templateUrl: 'servant/technique_edit.html',
        controller: 'servantTechniqueEditController'
    }).
    /*简历审核**/
    when('/customer/resume/list/app', {
        templateUrl: 'servant/review.html',
        controller: 'customerController'
    }).
    when('/customer/resume/info/:customer_id/app', {
        templateUrl: 'servant/resume_info.html',
        controller: 'customerResumeInfoController'
    }).
    when('/servant/talentPoolNew', {
        templateUrl: 'servant/talentPoolNew.html',
        controller: 'talentPoolNewController'
    }).
    when('/servant/talentPoolNewDetails/:servant_id', {
        templateUrl: 'servant/talentPoolNewDetails.html',
        controller: 'servantEditController'
    }).
    when('/servant/collaborateDetails/:servant_id', {
        templateUrl: 'servant/collaborateDetails.html',
        controller: 'servantEditController'
    }).
    when('/servant/applyList', {
        templateUrl: 'servant/applyList.html',
        controller: 'applyListController'
    }).
    when('/servant/applyListDetails/:servant_id', {
        templateUrl: 'servant/applyListDetails.html',
        controller: 'servantEditController'
    }).
    when('/servant/pensionInfo/:servant_id', {
        templateUrl: 'servant/pensionInfo.html',
        controller: 'servantEditController'
    });
}]);

function servantService($http, $location) {
    return {
        add: function (servant_id, app_id, data, recall) {

            var cerphoto = (angular.element('#J_UrlsCer').val() != '') ? (angular.element('#J_UrlsCer').val() + ',' + data.cerphoto) : data.cerphoto;
            var data = {
                name: data.name,
                sex: data.sex,
                nativePlace: data.nativePlace,
                identify: data.identify,
                birthday: data.birthday,
                photo: (angular.element('#J_Urls').val() != '') ? angular.element('#J_Urls').val() : data.photo,
                address: data.address,
                telephone: data.telephone,
                certificate: data.certificate,
                yikatong_number: data.yikatong_number,
                health_number: data.health_number,
                work_number: data.work_number,
                company_code: (data.company_code != undefined) ? data.company_code : '',
                company_name: (data.company_name != undefined) ? data.company_name : '',
                wotk_time: data.wotk_time,
                order_num: data.order_num,
                time_length: data.time_length,
                reasons: data.reasons,
                jobs: data.jobs,
                cers: data.cers,
                verifystatus: data.verifystatus,
                servantstatus: data.servantstatus,
                cerphoto: cerphoto,
                show: data.show,
                technique: data.technique
            };

            baas_jz.call({
                'http': $http,
                'method': 'post',
                'uri': 'servant/servant/edit/' + servant_id,
                'data': data,
                'success': function (d) {

                    if (d.status == '1') {
                        if (typeof recall == 'function') {
                            recall(d);
                        }
                    } else {

                        if (d.description) {
                            alert(d.description);
                        } else {
                            alert('网络原因，请求失败，请稍后重试');
                        }

                    }
                }
            });
        },
        save: function (data, recall) {
            baas_jz.call({
                'http': $http,
                'method': 'post',
                'uri': 'servant/servant/edit/' + data.servant_id,
                'data': data,
                'success': function (d) {

                    if (d.status == '1') {
                        if (typeof recall == 'function') {
                            recall(d);
                        }
                    } else {

                        if (d.description) {
                            alert(d.description);
                        } else {
                            alert('网络原因，请求失败，请稍后重试');
                        }

                    }
                }
            });
        },
        list: function (data, limit, page, app_id, fn) {
            var param = {
                limit: limit,
                page: page,
                name: (data != '' && data != undefined) ? data.name : '',
                identify: (data != '' && data != undefined) ? data.identify : '',
                address: (data != '' && data != undefined) ? data.address : '',
                telephone: (data != '' && data != undefined) ? data.telephone : '',
                nativePlace: (data != '' && data != undefined) ? data.nativePlace : '',
                from: (data != '' && data != undefined) ? data.from : '',
                sex: (data != '' && data != undefined) ? data.sex : '',
                cers: (data != '' && data != undefined) ? data.cers : '',
                technique: (data != '' && data != undefined) ? data.technique : '',
                jobs: (data != '' && data != undefined) ? data.jobs : '',
                manager_group_id: (data != '' && data != undefined) ? data.manager_group_id : '',
                servantstatus: (data != '' && data != undefined) ? data.servantstatus : '',
                serviceType3_code: (data != '' && data != undefined && data.serviceType3 != undefined) ? data.serviceType3.code : undefined,
                verifystatus: (data != '' && data != undefined) ? data.verifystatus : ''
            };
            baas_jz.call({
                'http': $http,
                'method': 'post',
                'uri': 'servant/servant/search',
                'data': param,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        },
        adminList: function (data,limit, page,  fn) {

            var param = {
                limit: limit,
                page: page,
                name: (data != '' && data != undefined) ? data.name : '',
                identify: (data != '' && data != undefined) ? data.identify : '',
                address: (data != '' && data != undefined) ? data.address : '',
                telephone: (data != '' && data != undefined) ? data.telephone : '',
                nativePlace: (data != '' && data != undefined) ? data.nativePlace : '',
                from: (data != '' && data != undefined) ? data.from : '',
                sex: (data != '' && data != undefined) ? data.sex : '',
                cers: (data != '' && data != undefined) ? data.cers : '',
                technique: (data != '' && data != undefined) ? data.technique : '',
                jobs: (data != '' && data != undefined) ? data.jobs : '',
                manager_group_id: (data != '' && data != undefined) ? data.manager_group_id : '',
                record_manager_id: typeof data.record_manager_id != undefined ? data.record_manager_id : '',
                servantstatus: (data != '' && data != undefined) ? data.servantstatus : '',
                serviceType3_code: (typeof data.serviceType3 === 'undefined' || data.serviceType3 == null) ? '' : data.serviceType3.code,
                verifystatus: (data != '' && data != undefined) ? data.verifystatus : ''
            };
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'servant/servant/admin/search',
                'data': param,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                        console.log(d);
                    }
                }
            });
        },
        searchList: function (data, limit, page, app_id, fn) {

            var param = {
                limit: limit,
                page: page,
                name: (data != '' && data != undefined) ? data.name : '',
                identify: (data != '' && data != undefined) ? data.identify : '',
                address: (data != '' && data != undefined) ? data.address : '',
                telephone: (data != '' && data != undefined) ? data.telephone : '',
                nativePlace: (data != '' && data != undefined) ? data.nativePlace : '',
                from: (data != '' && data != undefined) ? data.from : '',
                sex: (data != '' && data != undefined) ? data.sex : '',
                cers: (data != '' && data != undefined) ? data.cers : '',
                technique: (data != '' && data != undefined) ? data.technique : '',
                jobs: (data != '' && data != undefined) ? data.jobs : '',
                manager_group_id: (data != '' && data != undefined) ? data.manager_group_id : '',
                servantstatus: (data != '' && data != undefined) ? data.servantstatus : '',
                //serviceType2_code : (data != ''&& data != undefined)?data.serviceType2_code:'',
                serviceType3_code: (data != '' && data != undefined && data.serviceType3_code != undefined) ? data.serviceType3_code : undefined,
                verifystatus: (data != '' && data != undefined) ? data.verifystatus : ''
            };
            baas_jz.call({
                'http': $http,
                'method': 'post',
                // 'uri': 'servant/servant/list',
                'uri': 'servant/servant/search',
                'data': param,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                        console.log(d);
                    }
                }
            });
        },

        searchForPhone: function (data, fn) {
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'servant/servant/telephone/' + data.telephone,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                        console.log(d);
                    }
                }
            });
        },
        searchForIdentify: function (identify, fn) {
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'servant/servant/identify/' + identify,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                        console.log(d);
                    }
                }
            });
        },
        employ: function (servant_id, company_code, fn) {
            baas_jz.call({
                'http': $http,
                'method': 'post',
                data: {
                    company_code: company_code
                },
                'uri': 'servant/servant/add/employ_company/servant_id/' + servant_id,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                        console.log(d);
                    }
                }
            });
        },
        find_one: function (servant_id, app_id, fn) {
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'servant/servant/servant_id/' + servant_id,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        },
        delete: function (servant_id, app_id, fn) {
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'servant/servant/delete/' + servant_id,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }

        /**************************************************/
        , job_find_one: function (job_id, app_id, fn) {
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'servant/job/job_id/' + job_id,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
        , jobEdit: function (job_id, app_id, data, recall) {


            var data = {
                name: data.name,
                description: data.description
            };

            baas_jz.call({
                'http': $http,
                'method': 'post',
                'uri': 'servant/job/edit/' + job_id,
                'data': data,
                'success': function (d) {
                    if (d.status == '1') {
                        if (typeof recall == 'function') {
                            recall(d);
                        }
                    } else {
                        if (d.description) {
                            alert(d.description);
                        } else {
                            alert('网络原因，请求失败，请稍后重试');
                        }
                    }
                }
            });
        }

        , jobList: function (app_id, fn) {
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'servant/job/list',
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
        , jobDelete: function (job_id, app_id, fn) {
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'servant/job/delete/' + job_id,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
        /*证书******************************************************/
        , cer_find_one: function (cer_id, app_id, fn) {
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'servant/certificate/cer_id/' + cer_id,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
        , cerEdit: function (cer_id, app_id, data, recall) {

            var data = {
                name: data.name,
                description: data.description
            };

            baas_jz.call({
                'http': $http,
                'method': 'post',
                'uri': 'servant/certificate/edit/' + cer_id,
                'data': data,
                'success': function (d) {
                    if (d.status == '1') {
                        if (typeof recall == 'function') {
                            recall(d);
                        }
                    } else {
                        if (d.description) {
                            alert(d.description);
                        } else {
                            alert('网络原因，请求失败，请稍后重试');
                        }
                    }
                }
            });
        }

        , cerList: function (app_id, fn) {
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'servant/certificate/list',

                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
        , cerDelete: function (cer_id, app_id, fn) {
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'servant/certificate/delete/' + cer_id,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
        /**************************************************/
        , technique_find_one: function (technique_id, app_id, fn) {
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'servant/technique/technique_id/' + technique_id,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
        , techniqueEdit: function (technique_id, app_id, data, recall) {

            var data = {
                name: data.name,
                description: data.description
            };

            baas_jz.call({
                'http': $http,
                'method': 'post',
                'uri': 'servant/technique/edit/' + technique_id,
                'data': data,
                'success': function (d) {
                    if (d.status == '1') {
                        if (typeof recall == 'function') {
                            recall(d);
                        }
                    } else {
                        if (d.description) {
                            alert(d.description);
                        } else {
                            alert('网络原因，请求失败，请稍后重试');
                        }
                    }
                }
            });
        }

        , techniqueList: function (app_id, fn) {
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'servant/technique/list',
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
        , techniqueDelete: function (technique_id, app_id, fn) {
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'servant/technique/delete/' + technique_id,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
        , companyList: function (app_id, fn) {
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'company/company/list',
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        },
        import: function (path, app_id, fn) {
            var url = WEB_URL + '/servant/servant/import';
            url += '?app_id=' + app_id;
            data = {
                path: path,
                tag: 1
            };

            var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
            if (manager.company_code != undefined) {
                data.company_code = manager.company_code;
                data.company_name = manager.company_name;
                data.tag = 0;
            }
            if (typeof manager.manager_group_id != 'undefined') {
                data.record_manager_group_id = manager.manager_group_id;
            }
            if (typeof manager.manager_id != 'undefined') {
                data.record_manager_id = manager.manager_id;
            }
            if (typeof manager.manager_account != 'undefined') {
                data.record_manager_account = manager.manager_account;
            }
            if (manager.isPension) {
                data.manager_group_id = manager.manager_group_id;
            }
            console.log(data);
            baas_jz.call({
                'http': $http,
                'method': 'post',
                'data': data,
                'uri': 'servant/servant/import',
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
        //审核简历
        , resumeList: function (current_page, app_id, fn) {
            var param = {
                page: current_page
            };
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'customer/index/resume/list',
                'data': param,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
        , info: function (app_id, customer_id, fn) {
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'customer/index/customer/' + customer_id,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
        , review: function (app_id, customer_name, fn) {
            var data = {
                username: customer_name
            };
            baas_jz.call({
                'http': $http,
                'method': 'post',
                'uri': 'servant/servant/review/resume/' + customer_name,
                'data': data,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }
        , talentPoolList: function (data, limit, page, app_id, fn) {

            var param = {
                limit: limit,
                page: page,
                name: (data != '' && data != undefined) ? data.username : '',
                identify: (data != '' && data != undefined) ? data.identify : '',
                address: (data != '' && data != undefined) ? data.address : '',
                telephone: (data != '' && data != undefined) ? data.telephone : '',
                nativePlace: (data != '' && data != undefined) ? data.nativePlace : '',
                from: (data != '' && data != undefined) ? data.from : '',
                sex: (data != '' && data != undefined) ? data.sex : '',
                cers: (data != '' && data != undefined) ? data.cers : '',
                technique: (data != '' && data != undefined) ? data.technique : '',
                service: (data != '' && data != undefined) ? data.service : '',
                servantstatus: (data != '' && data != undefined) ? data.servantstatus : '',
                verifystatus: (data != '' && data != undefined) ? data.verifystatus : '',
                birthday: (data != '' && data != undefined) ? data.birthday : '',
                serviceType3_code: (data != '' && data != undefined && data.service3 != undefined) ? data.service3.code : ''
            };
            baas_jz.call({
                'http': $http,
                'method': 'get',
                // 'uri': 'servant/servant/list',
                'uri': 'servant/servant/pool/search',
                'data': param,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        },
        addTalentPool: function (servant_id, app_id, data, recall) {


            var data = {
                to_company_name: data.personCompanyName,
                to_company_code: data.personCompanyCode,
                to_company_servant_id: data.servant_id,
                order_id: data.orderId,
                send_company_salary_rate: data.Proportion,
                to_company_salary_rate: data.Proportion2,
                send_company_code: data.newCompanyCode,
                send_company_name: data.newCompanyName

            };

            baas_jz.call({
                'http': $http,
                'method': 'post',
                'uri': 'company/cooperation/edit/' + servant_id,
                'data': data,
                'success': function (d) {

                    if (d.status == '1') {
                        if (typeof recall == 'function') {
                            recall(d);
                        }

                    } else {

                        if (d.description) {
                            alert(d.description);
                        } else {
                            alert('网络原因，请求失败，请稍后重试');
                        }

                    }
                }
            });
        },
        collaBorateList: function (data, limit, page, app_id, fn) {
            console.log('to_company_code==' + data.to_company_code);
            var param = {
                limit: limit,
                page: page,
                send_company_telephone: (data != '' && data != undefined) ? data.telePhone : undefined,
                send_company_name: (data != '' && data != undefined) ? data.company : undefined,
                to_company_servant_name: (data != '' && data != undefined) ? data.userName : undefined,
                serviceType1_code: (data != '' && data != undefined && data.serviceType1 != undefined) ? data.serviceType1.code : undefined,
                serviceType2_code: (data != '' && data != undefined && data.serviceType2 != undefined) ? data.serviceType2.code : undefined,
                serviceType3_code: (data != '' && data != undefined && data.serviceType3 != undefined) ? data.serviceType3.code : undefined,
                to_company_code: (data != '' && data != undefined) ? data.to_company_code : undefined,
            };
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'company/cooperation/list',
                'data': param,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        },
        applyList: function (data, limit, page, app_id, fn) {

            var param = {
                limit: limit,
                page: page,
                send_company_telephone: (data != '' && data != undefined) ? data.telePhone : undefined,
                send_company_name: (data != '' && data != undefined) ? data.company : undefined,
                to_company_name: (data != '' && data != undefined) ? data.to_company_name : undefined,
                to_company_code: (data != '' && data != undefined) ? data.to_company_code : undefined,
                to_company_servant_name: (data != '' && data != undefined) ? data.userName : undefined,
                serviceType1_code: (data != '' && data != undefined && data.serviceType1 != undefined) ? data.serviceType1.code : undefined,
                serviceType2_code: (data != '' && data != undefined && data.serviceType2 != undefined) ? data.serviceType2.code : undefined,
                serviceType3_code: (data != '' && data != undefined && data.serviceType3 != undefined) ? data.serviceType3.code : undefined,
                order_id: (data != '' && data != undefined) ? data.orderId : undefined,
                status: (data != '' && data != undefined) ? data.servantstatus : undefined,
            };
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'company/cooperation/list',
                'data': param,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        },
        applyListOrderCount: function (data, fn) {

            var param = {
                send_company_telephone: (data != '' && data != undefined) ? data.telePhone : undefined,
                to_company_name: (data != '' && data != undefined) ? data.personCompanyName : undefined,
                to_company_code: (data != '' && data != undefined) ? data.personCompanyCode : undefined,
                to_company_servant_name: (data != '' && data != undefined) ? data.userName : undefined,
                serviceType1_code: (data != '' && data != undefined) ? data.serviceType1code : undefined,
                serviceType2_code: (data != '' && data != undefined) ? data.serviceType2code : undefined,
                order_id: (data != '' && data != undefined) ? data.orderId : undefined,
                status: (data != '' && data != undefined) ? data.servantstatus : undefined,
            };
            baas_jz.call({
                'http': $http,
                'method': 'get',
                'uri': 'company/cooperation/list',
                'data': param,
                'success': function (d) {
                    if (typeof fn == 'function') {
                        fn(d);
                    }
                }
            });
        }

    };

}

app.factory('servantService', ['$http', '$location', servantService]);

app.controller('servantController', ['$scope', '$routeParams', '$window', '$location', '$http', 'servantService', servantController]);
app.controller('servantListController', ['$scope', '$rootScope', '$routeParams', '$window', '$location', '$http', 'servantService', 'orderService', servantListController]);
app.controller('servantAdminListController', ['$scope', '$rootScope', '$routeParams', '$window', '$location', '$http', 'servantService', 'orderService', servantAdminListController]);

app.controller('servantEditController', ['$scope', '$rootScope', '$routeParams', '$window', '$location', '$http', 'servantService', 'orderService', servantEditController]);

app.controller('servantJobListController', ['$scope', '$routeParams', '$window', '$location', '$http', 'servantService', servantJobListController]);
app.controller('servantJobEditController', ['$scope', '$routeParams', '$window', '$location', '$http', 'servantService', servantJobEditController]);

app.controller('servantCerListController', ['$scope', '$routeParams', '$window', '$location', '$http', 'servantService', servantCerListController]);
app.controller('servantCerEditController', ['$scope', '$routeParams', '$window', '$location', '$http', 'servantService', servantCerEditController]);

app.controller('servantTechniqueListController', ['$scope', '$routeParams', '$window', '$location', '$http', 'servantService', servantTechniqueListController]);
app.controller('servantTechniqueEditController', ['$scope', '$routeParams', '$window', '$location', '$http', 'servantService', servantTechniqueEditController]);
app.controller('collaborateController', ['$scope', '$http', '$routeParams', '$window', '$location', '$rootScope', 'servantService', collaborateController]);
app.controller('talentPoolController', ['$scope', '$routeParams', '$window', '$location', '$http', 'servantService', collaborateController]);
app.controller('talentPoolNewController', ['$scope', '$rootScope', '$routeParams', '$window', '$location', '$http', 'servantService', servantTalentPoolNewController]);

app.controller('customerController', ['$scope', '$routeParams', '$window', '$location', '$http', 'servantService', customerController]);
app.controller('customerResumeInfoController', ['$scope', '$routeParams', '$window', '$location', '$http', 'servantService', customerResumeInfoController]);
app.controller('applyListController', ['$scope', '$routeParams', '$window', '$location', '$http', 'servantService', applyListController]);

function servantEditController($scope, $rootScope, $routeParams, $window, $location, $http, servantService, orderService) {
    $.getScript('../module/controller/servant/kissy-servant.js');
    $.getScript('../module/controller/servant/kissy-cer.js');
    $scope.servant_id = $routeParams.servant_id;
    $scope.app_id = $routeParams.app_id;
    $scope.URL = WEB_URL;

    var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
    $scope.isCompanyAcount = typeof manager.company_code === "undefined" || manager.company_code == null ? false : true;

    $scope.job_list = function () {
        servantService.jobList($scope.app_id, function (json) {
            $scope.jobs = json['list'];
            if (typeof $scope.servant.jobs != 'undefined') {
                for (var i = 0; i < $scope.jobs.length; i++) {
                    for (var j = 0; j < $scope.servant.jobs.length; j++) {
                        if ($scope.jobs[i].name == $scope.servant.jobs[j]) {
                            $scope.jobs[i].chk = true;
                        }
                    }
                }
            }
        });
    };
    $scope.serviceItemList = [];
    //服务项目
    $scope.service_list = function () {
        $scope.serviceList = [];
        orderService.serviceList("0", function (d) {
            $scope.list = d.list;
            for (i = 0; i < d.list.length; i++) {
                var item = d.list[i];
                var service = {service1: item, service2: [], service3: []};
                $scope.serviceList.push(service);
                orderService.serviceList(item.code, function (dd) {
                    for (k = 0; k < $scope.serviceList.length; k++) {
                        var service1 = ($scope.serviceList[k]).service1;
                        if (dd.list.length > 0) {
                            if (service1.code == (dd.list[0]).parent_code) {
                                ($scope.serviceList[k]).service2 = dd.list;
                                break;
                            }
                        }
                    }
                    for (j = 0; j < dd.list.length; j++) {
                        var it = (dd.list)[j];
                        orderService.serviceList(it.code, function (dt) {
                            for (m = 0; m < dt.list.length; m++) {
                                var sv = dt.list[m];
                                if ($scope.servant != undefined && $scope.servant.serviceType3_code != undefined) {
                                    for (k = 0; k < $scope.servant.serviceType3_code.length; k++) {
                                        if ($scope.servant.serviceType3_code[k] ==
                                            sv.code) {
                                            sv.chk = true;
                                        }
                                    }
                                }
                            }

                            for (m = 0; m < $scope.serviceList.length; m++) {
                                var service2List = ($scope.serviceList[m]).service2;
                                for (n = 0; n < service2List.length; n++) {
                                    var sce = service2List[n];
                                    if (dt.list.length > 0 && sce.code == (dt.list[0]).parent_code) {
                                        ($scope.serviceList[m]).service3.push(dt.list);
                                        break;
                                    }
                                }

                            }
                        });
                    }
                });
            }
        });
    };
    //获得服务项目大中小类 by gxl 20150928


    $scope.serviceListNew = function () {

        var param = {};
        baas_jz.call({
            'http': $http,
            'method': 'get',
            'uri': 'console/service/map',
            'data': 'param',
            'success': function (d) {
                $scope.serviceListNew1 = d;
                if (typeof $scope.serviceListNew1 != 'undefined') {

                    for (var i = 0; i < $scope.serviceListNew1.length; i++) {
                        //console.log('test==='+$scope.serviceListNew1[i].child);
                        var list1 = $scope.serviceListNew1[i].child;
                        for (var a = 0; a < $scope.serviceListNew1[i].child.length; a++) {
                            //console.log("test===01=="+list1[a].child);
                            var list2 = $scope.serviceListNew1[i].child[a].child;
                            //console.log("test===02=="+list2);
                            for (var b = 0; b < $scope.serviceListNew1[i].child[a].child.length; b++) {
                                //console.log("test===02=="+list2[b].code);
                                if ($scope.servant != undefined && $scope.servant.serviceType3_code != undefined) {
                                    //console.log("test===test");
//              						console.log("test==="+$scope.serviceListNew1[i].child[a].child[b].code);
                                    for (c = 0; c < $scope.servant.serviceType3_code.length; c++) {
                                        if ($scope.servant.serviceType3_code[c] ==
                                            $scope.serviceListNew1[i].child[a].child[b].code) {
                                            $scope.serviceListNew1[i].child[a].child[b].chk = true;
                                            //console.log("test===test");
                                        }
                                    }

                                }
                            }
                        }

                    }


                }

            }
        });
    }
    $scope.serviceListNew();
    $scope.technique_list = function () {
        servantService.techniqueList($scope.app_id, function (json) {
            $scope.technique = json['list'];
            if (typeof $scope.servant.technique != 'undefined') {
                for (var i = 0; i < $scope.technique.length; i++) {
                    for (var j = 0; j < $scope.servant.technique.length; j++) {
                        if ($scope.technique[i].name == $scope.servant.technique[j]) {
                            $scope.technique[i].chk = true;
                        }
                    }
                }
            }
        });
    };
    $scope.cer_list = function () {
        servantService.cerList($scope.app_id, function (json) {
            $scope.cers = json['list'];
            if (typeof $scope.servant.cers != 'undefined') {
                for (var i = 0; i < $scope.cers.length; i++) {
                    for (var j = 0; j < $scope.servant.cers.length; j++) {
                        if ($scope.cers[i].name == $scope.servant.cers[j]) {
                            $scope.cers[i].chk = true;
                        }
                    }
                }
            }
        });
    };
    $scope.company_list = function (company_code) {
        servantService.companyList($scope.app_id, function (json) {
            $scope.companys = json['list'];
            if (typeof $scope.servant.company != 'undefined') {
                for (var i = 0; i < $scope.companys.length; i++) {
                    if (company_code == $scope.companys[i].code) {
                        $scope.servant.company = $scope.companys[i];
                    }
                }
            }
        });
    };

    $scope.find = function () {
        if ($scope.servant_id == 0) {
            $scope.servant = {};
            $scope.company_list();
            $scope.job_list();
            $scope.technique_list();
            $scope.cer_list();
            $scope.servant.servantstatus = "待岗";
            $scope.service_list();
            return;
        }
        servantService.find_one($scope.servant_id, $scope.app_id, function (json) {
            if (json.status) {
                $scope.servant = json.servant;
                $scope.servant.maxCompany_code = json.servant.company_code;
                console.log(json.servant);
                if (json.servant.cerphoto != '' && json.servant.cerphoto != undefined) {
                    var cerphotos = Array();
                    cerphotos = json.servant.cerphoto.split(',');
                    $scope.cerphotos = cerphotos;
                }
                $scope.company_list();
                $scope.service_list();
                $scope.technique_list();
                $scope.cer_list();
            }
        });
    };
    $scope.find();

    $scope.save = function (servant) {
        console.log("====" + servant);
        if (typeof servant == 'undefined' ||
            typeof servant.name == 'undefined' ||
            servant.name.replace(/^(\s+)|(\s+)$/, '').length == 0) {

            alert('名称不能为空');
            return;
        }
        if (typeof servant == 'undefined' ||
            typeof servant.identify == 'undefined' ||
            servant.identify.replace(/^(\s+)|(\s+)$/, '').length == 0) {

            alert('身份证不能为空');
            return;
        }
        if (typeof servant == 'undefined' ||
            typeof servant.telephone == 'undefined' ||
            servant.telephone.replace(/^(\s+)|(\s+)$/, '').length == 0) {

            alert('联系电话不能为空');
            return;
        }
        var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
        console.log(manager);
        if (manager.company_code != undefined) {
            servant.company_code = manager.company_code;
            servant.company_name = manager.company_name;
            servant.company = manager.company_name;
        }
        // by gxl 20150929 取消本地获取company info
//        if(localStorage.getItem("company")!=""){
//         		servant.company_name=localStorage.getItem("company");
//         }
//         if(localStorage.getItem("company_code")!=""){
//         		servant.company_code=localStorage.getItem("company_code");
//         		servant.company=localStorage.getItem("company_code");
//         }

        servant.technique = [];
        servant.cers = [];
        for (var i = 0; i < $scope.cers.length; i++) {
            if ($scope.cers[i].chk) {
                servant.cers.push($scope.cers[i].name);
            }
        }
        for (var i = 0; i < $scope.technique.length; i++) {
            if ($scope.technique[i].chk) {
                servant.technique.push($scope.technique[i].name);
            }
        }

        servant.serviceType3_code = [];
        servant.serviceType3_title = [];
        /***
         *获取选中的 service3
         */
//      for( var i = 0; i < $scope.serviceList.length; i ++ ){
//      	 var service3List=($scope.serviceList[i]).service3;
//
//      	 for (j=0;j<service3List.length;j++) {
//      	 		for(k=0;k<service3List[j].length;k++){
//      	 			var service3=service3List[j][k];
//      	 			if(service3.chk !=undefined && service3.chk ){
//			                servant.serviceType3_code.push( service3.code );
//			                servant.serviceType3_title.push(service3.title );
//		            	}
//      	 		}
//
//      	 }
//      }


        for (var i = 0; i < $scope.serviceListNew1.length; i++) {
            var list1 = $scope.serviceListNew1[i].child;
            for (var a = 0; a < $scope.serviceListNew1[i].child.length; a++) {
                var list2 = $scope.serviceListNew1[i].child[a].child;
                for (var b = 0; b < $scope.serviceListNew1[i].child[a].child.length; b++) {
                    if ($scope.serviceListNew1[i].child[a].child[b].chk != undefined &&
                        $scope.serviceListNew1[i].child[a].child[b].chk) {
                        servant.serviceType3_code.push($scope.serviceListNew1[i].child[a].child[b].code);
                        servant.serviceType3_title.push($scope.serviceListNew1[i].child[a].child[b].title);
                    }

                }
            }

        }


        if (servant.verifystatus == undefined) {
            servant.verifystatus = "未审核";
        }
        servant.servant_id = $scope.servant_id;
        servant.cerphoto = (angular.element('#J_UrlsCer').val() != '' && angular.element('#J_UrlsCer').val() != undefined) ? (angular.element('#J_UrlsCer').val() + ',' + servant.cerphoto) : servant.cerphoto;
        servant.photo = (angular.element('#J_Urls').val() != '') ? angular.element('#J_Urls').val() : servant.photo;
        servantService.save(servant, function (json) {
            if (json.status) {
                $scope.servant = json.servant;
                if ($scope.isCompanyAcount) {
                    $location.path('/servant/list');
                } else {
                    $location.path('/servant/admin/list');
                }

            }

        });

    };
    $scope.saveNew = function (servant) {
        console.log("====" + servant);

        if (typeof servant == 'undefined' ||
            typeof servant.name == 'undefined' ||
            servant.name.replace(/^(\s+)|(\s+)$/, '').length == 0) {

            alert('名称不能为空');
            return;
        }
        if (typeof servant == 'undefined' ||
            typeof servant.identify == 'undefined' ||
            servant.identify.replace(/^(\s+)|(\s+)$/, '').length == 0) {

            alert('身份证不能为空');
            return;
        }
        if (typeof servant == 'undefined' ||
            typeof servant.telephone == 'undefined' ||
            servant.telephone.replace(/^(\s+)|(\s+)$/, '').length == 0) {

            alert('联系电话不能为空');
            return;
        }

        servant.technique = [];
        servant.cers = [];
        for (var i = 0; i < $scope.cers.length; i++) {
            if ($scope.cers[i].chk) {
                servant.cers.push($scope.cers[i].name);
            }
        }
        for (var i = 0; i < $scope.technique.length; i++) {
            if ($scope.technique[i].chk) {
                servant.technique.push($scope.technique[i].name);
            }
        }

        servant.serviceType3_code = [];
        servant.serviceType3_title = [];


        for (var i = 0; i < $scope.serviceListNew1.length; i++) {
            var list1 = $scope.serviceListNew1[i].child;
            for (var a = 0; a < $scope.serviceListNew1[i].child.length; a++) {
                var list2 = $scope.serviceListNew1[i].child[a].child;
                for (var b = 0; b < $scope.serviceListNew1[i].child[a].child.length; b++) {
                    if ($scope.serviceListNew1[i].child[a].child[b].chk != undefined &&
                        $scope.serviceListNew1[i].child[a].child[b].chk) {
                        servant.serviceType3_code.push($scope.serviceListNew1[i].child[a].child[b].code);
                        servant.serviceType3_title.push($scope.serviceListNew1[i].child[a].child[b].title);
                    }

                }
            }

        }

        if (servant.maxCompany_code != undefined) {
            for (i = 0; i < servant.employ_company.length; i++) {
                var emp = servant.employ_company[i];
                if (servant.maxCompany_code == emp.code) {
                    servant.company_code = emp.code;
                    servant.company_name = emp.name;
                    break;
                }
            }
        }
        delete servant["maxCompany_code"];

        if (servant.verifystatus == undefined) {
            servant.verifystatus = "未审核";
        }
        servant.servant_id = $scope.servant_id;
        servant.cerphoto = (angular.element('#J_UrlsCer').val() != '' && angular.element('#J_UrlsCer').val() != undefined) ? (angular.element('#J_UrlsCer').val() + ',' + servant.cerphoto) : servant.cerphoto;
        servant.photo = (angular.element('#J_Urls').val() != '') ? angular.element('#J_Urls').val() : servant.photo;

        servantService.save(servant, function (json) {
            if (json.status) {
                $scope.servant = json.servant;
                if ($scope.isCompanyAcount) {
                    $location.path('/servant/list');
                } else {
                    $location.path('/servant/admin/list');
                }
            }

        });

    };
    $scope.edit = function (servant) {

        if (typeof servant == 'undefined' ||
            typeof servant.name == 'undefined' ||
            servant.name.replace(/^(\s+)|(\s+)$/, '').length == 0) {

            alert('名称不能为空');
            return;
        }
        if (typeof servant.company != 'undefined' &&
            servant.company != "") {
            servant.company_code = servant.company.code;
            servant.company_name = servant.company.name;
            servant.company = servant.company.code;

        }
        servant.technique = [];
        servant.cers = [];
        for (var i = 0; i < $scope.cers.length; i++) {
            if ($scope.cers[i].chk) {
                servant.cers.push($scope.cers[i].name);
            }
        }
        for (var i = 0; i < $scope.technique.length; i++) {
            if ($scope.technique[i].chk) {
                servant.technique.push($scope.technique[i].name);
            }
        }
        servant.serviceType2_code = [];
        servant.serviceType2_title = [];
        for (var i = 0; i < $scope.serviceItemList.length; i++) {
            if ($scope.serviceItemList[i].chk) {
                servant.serviceType2_code.push($scope.serviceItemList[i].code);
                servant.serviceType2_title.push($scope.serviceItemList[i].title);
            }
        }
        servantService.add($scope.servant_id, $scope.app_id, servant, function (json) {
            if (json.status) {
                $scope.servant = json.servant;
                if ($scope.isCompanyAcount) {
                    $location.path('/servant/list');
                } else {
                    $location.path('/servant/admin/list');
                }
            }

        });


    };

    $scope.employ = {
        edit_status: 0,
        employ_status: 0
    };
    // 检查 身份证
    $scope.checkIdentify = function (identify) {
        $scope.employ.edit_status = 0;
        $scope.employ.employ_status = 0;

        var company_code = localStorage.getItem('company_code');

        var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
        if (manager.company_code != undefined) {
            company_code = manager.company_code;
        }
        servantService.searchForIdentify(identify, function (json) {
            if (json.status) {
                $scope.employ.servant = json.servant;
                $scope.employ.edit_status = 1;
                if (json.servant.employ_company != undefined) {
                    var len = json.servant.employ_company.length;
                    for (var i = 0; i < len; i++) {
                        if (json.servant.employ_company[i].code == company_code) {
                            $scope.employ.employ_status = 1;
                        }
                    }
                }

            }
        });
    };
    $scope.employ = function (servant) {


        var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
        var company_code = manager.company_code;
        servantService.employ(servant._id.$id, company_code, function (json) {
            if (json.status) {
                alert('挂靠成功');
                $scope.servant = servant;
                console.log($scope.servant);
            } else {
                alert(json.description);
            }
            $scope.checkIdentify(servant.identify);
        });
    };
}

function servantController($scope, $routeParams, $window, $location, $http, servantService) {


}

function servantListController($scope, $rootScope, $routeParams, $window, $location, $http, servantService, orderService) {
    $scope.app_id = app_id;
    $scope.URL = WEB_URL;

    $scope.selectedPage = 1;
    $scope.search = {
        selectedPage: $scope.selectedPage
    };

    var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
    $scope.isCompanyAcount = typeof manager.company_code === "undefined" || manager.company_code == null ? false : true;
    if (manager != undefined && manager.isPension) {
        $scope.search.manager_group_id = manager.manager_group_id;
    }

    //检查是否为挂靠企业
    var checkEmployed = function (list) {
        for (var i in list) {
            list[i].isEmployed = false;
            if (list[i].company_code != manager.company_code) {
                var employ_company = list[i].employ_company;
                for (j in employ_company) {
                    if (employ_company[j].code == manager.company_code) {
                        list[i].isEmployed = true;
                    }
                }
            }
        }
    };

    $scope.limit = 20;
    $scope.getList = function () {
        servantService.list($scope.search, $scope.limit, $scope.selectedPage, $scope.app_id, function (json) {
            console.log(json.list);
            $scope.list = json.list;
            checkEmployed($scope.list);
            $scope.count = json.count;
            $scope.currentPage = 1;
            $scope.getPage(json.count);
        });
    };
    $scope.searchList = function (data) {
        alert(123);
        var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
        if (manager != undefined && manager.isPension) {
            data.manager_group_id = manager.manager_group_id;
        }
        $scope.selectedPage = 1;
        servantService.list(data, $scope.limit, $scope.selectedPage, $scope.app_id, function (json) {
            $scope.list = json.list;
            checkEmployed($scope.list);
            $scope.count = json.count;
            $scope.currentPage = 1;
            $scope.getPage(json.count);
        });
    };
    $scope.getList();

    /*搜索时获取技能、证书、服务项目列表  start*/
    $scope.technique_list = function () {
        servantService.techniqueList($scope.app_id, function (json) {
            $scope.techniquelist = json['list'];
        });
    };
    $scope.technique_list();
    $scope.cer_list = function () {
        servantService.cerList($scope.app_id, function (json) {
            $scope.cerslist = json['list'];
        });
    };
    $scope.cer_list();

    //服务项目
    $scope.serviceItemList = [];
    $scope.service_list = function () {
        orderService.serviceList("0", function (d) {
            for (var i = 0; i < d.list.length; i++) {
                var item = d.list[i];
                orderService.serviceList(item.code, function (dd) {
                    for (j = 0; j < dd.list.length; j++) {
                        $scope.serviceItemList.push(dd.list[j]);
                    }
                });
            }
        });
    };
    $scope.service_list();

    //服务项目new by gxl 20151013
    $scope.getserviceType1 = function (code) {
        var data = {
            'code': (code != '' && code != undefined) ? code : ''
        };
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'data': data,
            'uri': 'order/orderList/serviceList',
            'success': function (d) {
                $scope.serviceList1 = d.list;
                $scope.serviceList2 = [];
                $scope.search.serviceType2 = {};
                $scope.serviceList3 = [];
                $scope.search.serviceType3 = {};
            }
        });
    };
    //中类
    $scope.getserviceType2 = function (code) {
        var data = {
            'code': (code != '' && code != undefined) ? code : ''
        };
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'data': data,
            'uri': 'order/orderList/serviceList',
            'success': function (d) {
                $scope.serviceList2 = d.list;
                $scope.serviceList3 = [];
                $scope.search.serviceType3 = {};
            }
        });
    };
    //小类
    $scope.getserviceType3 = function (code) {
        var data = {
            'code': (code != '' && code != undefined) ? code : ''
        };
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'data': data,
            'uri': 'order/orderList/serviceList',
            'success': function (d) {
                $scope.serviceList3 = d.list;
            }
        });
    };
    $scope.getserviceType1('0');

    /*分页*/
    $scope.changePage = function (p) {
        $scope.search.selectedPage = p;
        servantService.list($scope.search, $scope.limit, p, $scope.app_id, function (json) {
            $scope.list = json.list;
            checkEmployed($scope.list);
            $scope.count = json.count;
            $scope.currentPage = p;
            $scope.getPage(json.count);
        });
    };
    $scope.getPage = function (count) {
        $scope.pageList = [];
        var limit = 20; //返回的条数
        $scope.totalPage = parseInt(count / limit); //总页数
        $scope.totalPage += count % limit >= 1 ? 1 : 0; //判断页数是否是整数
        var start = ($scope.currentPage - 2) >= 1 ? $scope.currentPage - 2 : 1; //首页
        var end = ($scope.currentPage + 2) <= $scope.totalPage ? $scope.currentPage + 2 : $scope.totalPage; //尾页

        $scope.frontdotShow = start > 1 ? true : false; //前面的省略号
        $scope.backdotShow = end < $scope.totalPage ? true : false; //后面的省略号
        for (var i = start; i <= end; i++) {
            $scope.pageList.push({
                number: i,
                active: i == $scope.currentPage ? 'active' : ''
            }); //给点击的按钮添加class
        }
    };

    $scope.delete = function (servant) {
        servantService.delete(servant.servant_id, $scope.app_id, function (json) {
            if (json.status) {
                $scope.getList();
            } else {
                if (typeof json.description) {
                    alert(json.description);
                } else {
                    alert('由于网络原因，请稍后重试');
                }
            }
        });
    };

    $scope.import = function () {
        var path = angular.element('#filepath').val();
        servantService.import(path, $scope.app_id, function (json) {
            if (json.status) {
                alert(json.description);
                closeMod();
                $scope.getList();
            } else
                alert('导入失败，请重新导入');
        });
    };

    // 修改后返回 当前页 by gxl 20150928
    $scope.getListNew = function () {
        var currentPage = $scope.currentPage
        servantService.list($scope.search, $scope.limit, $scope.selectedPage, $scope.app_id, function (json) {
            $scope.list = json.list;
            checkEmployed($scope.list);
            $scope.changePage(currentPage);
        });
    };
    //////////////////////////pension  ----  add  servant////////////////////////////////////
    $scope.checkServant = function (servant_id, verifystatus) {
        var data = {
            verifystatus: verifystatus
        }
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'uri': 'servant/servant/edit/' + servant_id,
            'data': data,
            'success': function (d) {
                if (d.status == '1') {
                    $scope.getListNew();
                } else {

                    if (d.description) {
                        alert(d.description);
                    } else {
                        alert('网络原因，请求失败，请稍后重试');
                    }

                }
            }
        });
    }

    $scope.servantRes = {};
    $scope.searchForPhone = function (data) {
        servantService.searchForPhone(data, function (d) {
            $scope.servantRes = d;
        });
    }
    $scope.saveServant = function (data) {
        var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
        var params = {
            manager_id: manager.manager_id,
            servant_id: data._id.$id
        }

        servantService.save(params, function (d) {
            alert("添加成功");
            closeMod();

            $scope.getList();
            $scope.clearServant();
        });
    }
    $scope.clearServant = function () {
        $scope.servant = {};
        $scope.servantRes = {};
    }
    $scope.deleteServant = function (obj) {
        var params = {
            manager_id: "0",
            servant_id: obj.servant_id
        }
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'uri': 'servant/servant/servant/' + params.servant_id + "/manager/0",
            'data': params,
            'success': function (d) {
                if (d.status == '1') {
                    $scope.getList();
                } else {
                    if (d.description) {
                        alert(d.description);
                    } else {
                        alert('网络原因，请求失败，请稍后重试');
                    }

                }
            }
        });
    }
}

function servantJobEditController($scope, $routeParams, $window, $location, $http, servantService) {

    $scope.job_id = $routeParams.job_id;
    $scope.app_id = $routeParams.app_id;
    $scope.find = function () {
        if ($scope.job_id == 0) {
            return;
        }
        servantService.job_find_one($scope.job_id, $scope.app_id, function (json) {

            if (json.status) {
                $scope.job = json.job;
            }
        });
    };

    $scope.find();
    $scope.edit = function (job) {

        if (typeof job == 'undefined' ||
            typeof job.name == 'undefined' ||
            job.name.replace(/^(\s+)|(\s+)$/, '').length == 0) {

            alert('名称不能为空');
            return;
        }
        servantService.jobEdit($scope.job_id, $scope.app_id, job, function (json) {

            if (json.status) {
                $scope.job = json.job;
                $location.path('/servant/job/list');
            }

        });
    };

}

function servantJobListController($scope, $routeParams, $window, $location, $http, servantService) {

    $scope.app_id = $routeParams.app_id;
    $scope.getList = function () {
        servantService.jobList($scope.app_id, function (json) {
            $scope.list = json['list'];
        });
    };
    $scope.getList();

    $scope.delete = function (job) {
        servantService.jobDelete(job.job_id, $scope.app_id, function (json) {
            if (json.status) {
                $scope.getList();
            } else {
                if (typeof json.description) {
                    alert(json.description);
                } else {
                    alert('由于网络原因，请稍后重试');
                }
            }
        });
    };
}


function servantTechniqueEditController($scope, $routeParams, $window, $location, $http, servantService) {

    $scope.technique_id = $routeParams.technique_id;
    $scope.app_id = $routeParams.app_id;
    $scope.find = function () {
        if ($scope.technique_id == 0) {
            return;
        }
        servantService.technique_find_one($scope.technique_id, $scope.app_id, function (json) {

            if (json.status) {
                $scope.technique = json.technique;
            }
        });
    };

    $scope.find();
    $scope.edit = function (technique) {

        if (typeof technique == 'undefined' ||
            typeof technique.name == 'undefined' ||
            technique.name.replace(/^(\s+)|(\s+)$/, '').length == 0) {

            alert('名称不能为空');
            return;
        }
        servantService.techniqueEdit($scope.technique_id, $scope.app_id, technique, function (json) {

            if (json.status) {
                $scope.technique = json.technique;
                $location.path('/servant/technique/list');
            }

        });
    };

}

function servantTechniqueListController($scope, $routeParams, $window, $location, $http, servantService) {

    $scope.app_id = $routeParams.app_id;
    $scope.getList = function () {
        servantService.techniqueList($scope.app_id, function (json) {
            $scope.list = json['list'];
        });
    };
    $scope.getList();

    $scope.delete = function (technique) {
        servantService.techniqueDelete(technique.technique_id, $scope.app_id, function (json) {
            if (json.status) {
                $scope.getList();
            } else {
                if (typeof json.description) {
                    alert(json.description);
                } else {
                    alert('由于网络原因，请稍后重试');
                }
            }
        });
    };
}

function servantCerEditController($scope, $routeParams, $window, $location, $http, servantService) {

    $scope.cer_id = $routeParams.cer_id;
    $scope.app_id = $routeParams.app_id;
    $scope.find = function () {
        if ($scope.cer_id == 0) {
            return;
        }
        servantService.cer_find_one($scope.cer_id, $scope.app_id, function (json) {

            if (json.status) {
                $scope.cer = json.cer;
            }
        });
    };

    $scope.find();
    $scope.edit = function (cer) {

        if (typeof cer == 'undefined' ||
            typeof cer.name == 'undefined' ||
            cer.name.replace(/^(\s+)|(\s+)$/, '').length == 0) {

            alert('名称不能为空');
            return;
        }
        servantService.cerEdit($scope.cer_id, $scope.app_id, cer, function (json) {

            if (json.status) {
                $scope.cer = json.cer;
                $location.path('/servant/cer/list');
            }

        });
    };

}

function servantCerListController($scope, $routeParams, $window, $location, $http, servantService) {

    $scope.app_id = $routeParams.app_id;
    $scope.getList = function () {
        servantService.cerList($scope.app_id, function (json) {
            $scope.list = json['list'];
        });
    };
    $scope.getList();

    $scope.delete = function (cer) {
        servantService.cerDelete(cer.cer_id, $scope.app_id, function (json) {
            if (json.status) {
                $scope.getList();
            } else {
                if (typeof json.description) {
                    alert(json.description);
                } else {
                    alert('由于网络原因，请稍后重试');
                }
            }
        });
    };
}

/***
 * 合作请求审核  by 20150906 gxl
 * @param {Object} $scope
 * @param {Object} $routeParams
 * @param {Object} $window
 * @param {Object} $location
 * @param {Object} $http
 * @param {Object} servantService
 */
function collaborateController($scope, $http, $routeParams, $window, $location, $rootScope, servantService) {
    $scope.app_id = $routeParams.app_id;
    $scope.URL = WEB_URL;
    $scope.selectedPage = 1;
    $scope.data = {
        selectedPage: $scope.selectedPage

    };
    $scope.searchNew = [];

    $scope.searchNew.push({
        userName: '',
        serviceType1code: '',
        serviceType2code: '',
        to_company_code: ''
    });
    var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
    $scope.searchNew.to_company_code = manager.company_code;
    $scope.limit = 20;
    $scope.getList = function () {
        servantService.collaBorateList($scope.searchNew, $scope.limit, $scope.selectedPage, $scope.app_id, function (json) {
            $scope.list = json.list;
            $scope.count = json.count;
            $scope.currentPage = 1;
            $scope.getPage(json.count);
        });
    };
    $scope.getList();


    //搜索
    $scope.search = function (searchNew) {
        $scope.selectedPage = 1;
        servantService.collaBorateList($scope.searchNew, $scope.limit, $scope.selectedPage, $scope.app_id, function (json) {

            $scope.list = json.list;
            $scope.count = json.count;
            $scope.currentPage = 1;
            $scope.getPage(json.count);
        });
    };
    /*搜索时获取技能、证书、服务项目列表  start*/
    $scope.technique_list = function () {
        servantService.techniqueList($scope.app_id, function (json) {
            $scope.techniquelist = json['list'];
        });
    };
    $scope.technique_list();
    $scope.cer_list = function () {
        servantService.cerList($scope.app_id, function (json) {
            $scope.cerslist = json['list'];
        });
    };
    $scope.cer_list();

    //获取服务类型列表
    $scope.getserviceType1 = function (code) {
        var data = {
            'code': code
        };
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'data': data,
            'uri': 'order/orderList/serviceList',
            'success': function (d) {
                $scope.serviceList = d.list;
            }
        });


    }

    $scope.getserviceType1('0');
    $scope.getserviceType2 = function (code) {
        var data = {
            'code': (code != '' && code != undefined) ? code : ''
        };
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'data': data,
            'uri': 'order/orderList/serviceList',
            'success': function (d) {
                $scope.serviceList2 = d.list;
                $scope.searchNew.serviceType1code = code;
                $scope.searchNew.serviceType2code = '';
            }
        });


    }

    //小类
    $scope.getserviceType3 = function (code) {
        var data = {
            'code': (code != '' && code != undefined) ? code : ''
        };
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'data': data,
            'uri': 'order/orderList/serviceList',
            'success': function (d) {
                $scope.serviceList3 = d.list;
            }
        });


    }
//  $scope.getserviceType3=function(code){
//  	$scope.search.serviceType2code=code;
//  }
    //获得申请企业
    $scope.getCompanyList = function () {
        baas_jz.call({
            'http': $http,
            'method': 'get',
            'uri': 'company/company/list',
            'success': function (d) {
                $scope.companyList = d.list;

            }
        });


    }
    $scope.getCompanyList();
    /*分页*/
    $scope.changePage = function (p) {
        console.log("test-==-11" + $scope.searchNew);
        $scope.data.selectedPage = p;
        servantService.collaBorateList($scope.searchNew, $scope.limit, p, $scope.app_id, function (json) {
            $scope.list = json.list;
            $scope.count = json.count;
            $scope.currentPage = p;
            $scope.getPage(json.count);
        });
    };
    $scope.getPage = function (count) {
        $scope.pageList = [];
        var limit = 20; //返回的条数
        $scope.totalPage = parseInt(count / limit); //总页数
        $scope.totalPage += count % limit >= 1 ? 1 : 0; //判断页数是否是整数
        var start = ($scope.currentPage - 2) >= 1 ? $scope.currentPage - 2 : 1; //首页
        var end = ($scope.currentPage + 2) <= $scope.totalPage ? $scope.currentPage + 2 : $scope.totalPage; //尾页

        $scope.frontdotShow = start > 1 ? true : false; //前面的省略号
        $scope.backdotShow = end < $scope.totalPage ? true : false; //后面的省略号
        for (var i = start; i <= end; i++) {
            $scope.pageList.push({
                number: i,
                active: i == $scope.currentPage ? 'active' : ''
            }); //给点击的按钮添加class
        }
    }
    //审核操作
    $scope.editStatus = function (cooperation, status) {
        var data = {
            status: status

        };

        var data1 = {
            company_code: cooperation.send_company_code
        };
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'data': data,
            'uri': 'company/cooperation/edit/' + cooperation.cooperation_id,
            'success': function (d) {
                // 审核成功后向订单的服务列表中追加合作人员信息 by gxl 20151020
                baas_jz.call({
                    'http': $http,
                    'method': 'post',
                    'data': data1,
                    'uri': 'order/order/grab/' + cooperation.order_id + '/' + cooperation.to_company_servant_id,
                    'success': function (d) {
                        $scope.getList();
                    }
                });

            }
        });


    }


}

function talentPoolController($scope, $routeParams, $window, $location, $http, servantService) {

}

function servantReviewController($scope, $routeParams, $window, $location, $http, servantService) {

}

function customerController($scope, $routeParams, $window, $location, $http, servantService) {
    $scope.app_id = $routeParams.app_id;
    $scope.page = [];
    $scope.currentPage = 1;
    $scope.totalPage = 1;
    var limit = 20;
    /*分页*/
    $scope.changePage = function (p) {
        servantService.resumeList(p, $scope.app_id, function (json) {
            $scope.list = json.list;
            $scope.count = json.count;
            $scope.currentPage = p;
            $scope.getPage(json.count);
        });
    };
    $scope.getPage = function (count) {
        $scope.pageList = [];
        var limit = 20; //返回的条数
        $scope.totalPage = parseInt(count / limit); //总页数
        $scope.totalPage += count % limit >= 1 ? 1 : 0; //判断页数是否是整数
        var start = ($scope.currentPage - 2) >= 1 ? $scope.currentPage - 2 : 1; //首页
        var end = ($scope.currentPage + 2) <= $scope.totalPage ? $scope.currentPage + 2 : $scope.totalPage; //尾页

        $scope.frontdotShow = start > 1 ? true : false; //前面的省略号
        $scope.backdotShow = end < $scope.totalPage ? true : false; //后面的省略号
        for (var i = start; i <= end; i++) {
            $scope.pageList.push({
                number: i,
                active: i == $scope.currentPage ? 'active' : ''
            }); //给点击的按钮添加class
        }
    };
    $scope.getList = function () {
        $scope.currentPage = typeof arguments[0] != 'undefined' ? arguments[0] : 1;
        servantService.resumeList($scope.currentPage, $scope.app_id, function (json) {
            $scope.list = json['list'];
            $scope.count = json.count;
            $scope.getPage(json.count);
        });
    };

    $scope.getList();
}

function customerResumeInfoController($scope, $routeParams, $window, $location, $http, servantService) {
    $scope.app_id = $routeParams.app_id;
    $scope.customer_id = $routeParams.customer_id;
    $scope.getInfo = function () {
        servantService.info($scope.app_id, $scope.customer_id, function (json) {
            $scope.username = json.username;
            $scope.resume = json.resume;
        });
    };
    $scope.getInfo();

    $scope.edit = function (username) {
        servantService.review($scope.app_id, $scope.username, function (json) {
            $scope.status = json.status;
            console.log($scope.status);
            $location.path('/customer/resume/list/app');
        });
    };
}

function servantTalentPoolNewController($scope, $rootScope, $routeParams, $window, $location, $http, servantService) {

    $scope.app_id = $routeParams.app_id;
    $scope.URL = WEB_URL;
    $scope.selectedPage = 1;
    $scope.data = {
        selectedPage: $scope.selectedPage
    };
    $scope.limit = 20;
    $scope.getList = function () {
        servantService.talentPoolList($scope.search, $scope.limit, $scope.selectedPage, $scope.app_id, function (json) {
            $scope.list = json.list;
            console.log(json.list);
            $scope.count = json.count;
            $scope.currentPage = 1;
            $scope.getPage(json.count);
        });
    };
    $scope.getList();
    //搜索
    $scope.search = function (search) {
        $scope.selectedPage = 1;
        search.birthday = angular.element("#birthday").val();
        servantService.talentPoolList($scope.search, $scope.limit, $scope.selectedPage, $scope.app_id, function (json) {

            $scope.list = json.list;
            $scope.count = json.count;
            $scope.currentPage = 1;
            $scope.getPage(json.count);
        });
    };
    /*搜索时获取技能、证书、服务项目列表  start*/
    $scope.technique_list = function () {
        servantService.techniqueList($scope.app_id, function (json) {
            $scope.techniquelist = json['list'];
        });
    };
    $scope.technique_list();
    $scope.cer_list = function () {
        servantService.cerList($scope.app_id, function (json) {
            $scope.cerslist = json['list'];
        });
    };
    $scope.cer_list();
    //获取服务类型列表
    //大类
    $scope.getserviceType1 = function (code) {
        var data = {
            'code': (code != '' && code != undefined) ? code : ''
        };
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'data': data,
            'uri': 'order/orderList/serviceList',
            'success': function (d) {
                $scope.serviceList = d.list;
            }
        });


    }
    //中类
    $scope.getserviceType2 = function (code) {
        var data = {
            'code': (code != '' && code != undefined) ? code : ''
        };
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'data': data,
            'uri': 'order/orderList/serviceList',
            'success': function (d) {
                $scope.serviceList2 = d.list;
            }
        });


    }
    //小类
    $scope.getserviceType3 = function (code) {
        var data = {
            'code': (code != '' && code != undefined) ? code : ''
        };
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'data': data,
            'uri': 'order/orderList/serviceList',
            'success': function (d) {
                $scope.serviceList3 = d.list;
            }
        });


    }
    $scope.getserviceType1('0');

    $scope.job_list = function () {
        servantService.jobList($scope.app_id, function (json) {
            $scope.jobList = json['list'];
        });
    };
    $scope.job_list();
    /*分页*/
    $scope.changePage = function (p) {
        $scope.data.selectedPage = p;
        servantService.talentPoolList($scope.search, $scope.limit, p, $scope.app_id, function (json) {
            $scope.list = json.list;
            $scope.count = json.count;
            $scope.currentPage = p;
            $scope.getPage(json.count);
        });
    };
    $scope.getPage = function (count) {
        $scope.pageList = [];
        var limit = 20; //返回的条数
        $scope.totalPage = parseInt(count / limit); //总页数
        $scope.totalPage += count % limit >= 1 ? 1 : 0; //判断页数是否是整数
        var start = ($scope.currentPage - 2) >= 1 ? $scope.currentPage - 2 : 1; //首页
        var end = ($scope.currentPage + 2) <= $scope.totalPage ? $scope.currentPage + 2 : $scope.totalPage; //尾页

        $scope.frontdotShow = start > 1 ? true : false; //前面的省略号
        $scope.backdotShow = end < $scope.totalPage ? true : false; //后面的省略号
        for (var i = start; i <= end; i++) {
            $scope.pageList.push({
                number: i,
                active: i == $scope.currentPage ? 'active' : ''
            }); //给点击的按钮添加class
        }
    };

    $scope.add = [];
    $scope.add.push({
        personName: '',
        personCompanyName: '',
        newCompanyName: '',
        newCompanyCode: '',
        orderId: '',
        Proportion: '',
        Proportion2: '',
        servant_id: '',
        personCompanyCode: ''
    });

    $scope.getCompanyPersonInfo = function (personName, personCompanyName, personCompanyCode, servantId) {
        console.log(personName);
        console.log(personCompanyName);
        console.log(personCompanyCode);
        console.log(servantId);
        $scope.add.personName = personName;
        $scope.add.personCompanyName = personCompanyName;
        $scope.add.personCompanyCode = personCompanyCode;
        $scope.add.servant_id = servantId;
        var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
        $scope.add.newCompanyName = manager.company_name;
        $scope.add.newCompanyCode = manager.company_code;

    }
    /*订单合作申请
     */
    $scope.save = function (add) {
        if (typeof add == 'undefined' ||
            typeof add.orderId == 'undefined' ||
            add.orderId.replace(/^(\s+)|(\s+)$/, '').length == 0) {

            alert('订单id不能为空！');
            return;
        }
        if (typeof add == 'undefined' ||
            typeof add.Proportion == 'undefined' ||
            add.Proportion.replace(/^(\s+)|(\s+)$/, '').length == 0) {

            alert('申请公司比例不能为空！');
            return;
        }
        if (typeof add == 'undefined' ||
            typeof add.Proportion2 == 'undefined' ||
            add.Proportion2.replace(/^(\s+)|(\s+)$/, '').length == 0) {

            alert('被申请公司比例不能为空！');
            return;
        }
        var pro = parseInt(add.Proportion);
        var pro2 = parseInt(add.Proportion2);
        //alert(pro);
        if (pro + pro2 !== parseInt(100)) {
            alert('申请公司比例与被申请公司比例之和不为100%！');
            return;
        }

        //add 判断该用户使用广场人员是否超过三次   by  gxl 20150908
        console.log($scope.add);
        // 判断to_company_code是否为空
        if ($scope.add.personCompanyCode != '' && $scope.add.personCompanyCode != undefined) {


            $scope.servant_id = 0;
            servantService.addTalentPool($scope.servant_id, $scope.app_id, add, function (jn) {
                if (jn.status) {
                    alert('申请合作成功！');
                    $scope.add = [];
                    $scope.add.push({
                        personName: '',
                        personCompanyName: '',
                        newCompanyName: '',
                        newCompanyCode: '',
                        orderId: '',
                        Proportion: '',
                        Proportion2: '',
                        servant_id: '',
                        personCompanyCode: ''
                    });
                    //$scope.af=false;
                    closeMod();
                }

            });


        } else {
            // console.log('test========test');
            servantService.applyListOrderCount($scope.add, function (jo) {
                var personCount = jo.count;
                if (parseInt(personCount) >= 3) {
                    alert("订单id：" + add.orderId + "申请广场公共人员已经超过三次！不能在继续申请广场公共人员~");
                    $scope.add = [];
                    $scope.add.push({
                        personName: '',
                        personCompanyName: '',
                        newCompanyName: '',
                        orderId: '',
                        Proportion: '',
                        Proportion2: '',
                        servant_id: '',
                        personCompanyCode: ''
                    });
                    closeMod();
                } else {

                    $scope.servant_id = 0;
                    servantService.addTalentPool($scope.servant_id, $scope.app_id, add, function (jn) {
                        if (jn.status) {
                            alert('申请合作成功！');
                            $scope.add = [];
                            $scope.add.push({
                                personName: '',
                                personCompanyName: '',
                                newCompanyName: '',
                                newCompanyCode: '',
                                orderId: '',
                                Proportion: '',
                                Proportion2: '',
                                servant_id: '',
                                personCompanyCode: ''
                            });
                            //$scope.af=false;
                            closeMod();
                        }

                    });
                }
            });
        }


    };
    //$scope.af=false;
    //判断订单有效性! by gxl 20151019
    $scope.orderIdCheck = function (orderId) {
        if (orderId == undefined) {
            alert('请输入订单号!');
            angular.element('#orderId').focus();
            return;
        }
        var id = orderId.replace(/^(\s+)|(\s+)$/, '');
        //判断订单是否是 有效订单并且状态在开始服务前
        var data = {
            'order_id': id
        };
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'data': data,
            'uri': 'order/orderList/info',
            'success': function (d) {
                $scope.order = d.orderinfo;
                //console.log('test'+$scope.order);
                if ($scope.order.order_id == undefined) {
                    alert('请输入有效订单id!');
                    return;
                }

                if ($scope.order.status != '已接单') {
                    alert('此订单不允许申请合作!');
                    return;
                }

                // $scope.af=true;
            }
        });


    }

    $scope.$on('ngRepeatFinished', function (ngRepeatFinishedEvent) {
        init();
    });


}

/***
 * 合作请求审核  by 20150907 gxl
 * @param {Object} $scope
 * @param {Object} $routeParams
 * @param {Object} $window
 * @param {Object} $location
 * @param {Object} $http
 * @param {Object} servantService
 */
function applyListController($scope, $routeParams, $window, $location, $http, servantService) {
    $scope.app_id = $routeParams.app_id;
    $scope.URL = WEB_URL;
    $scope.selectedPage = 1;
    $scope.data = {
        selectedPage: $scope.selectedPage
    };

    $scope.limit = 20;
    $scope.getList = function () {
        servantService.applyList($scope.search, $scope.limit, $scope.selectedPage, $scope.app_id, function (json) {
            console.log('test===test');
            $scope.list = json.list;
            console.log(json.list);
            $scope.count = json.count;
            console.log('test===test' + $scope.count + "===" + json.count);
            $scope.currentPage = 1;
            $scope.getPage(json.count);
        });
    };
    $scope.getList();
    $scope.search = [];

    $scope.search.push({
        userName: '',
        serviceType1code: '',
        serviceType2code: '',
        to_company_name: ''
    });

    //搜索
    $scope.search = function (search) {

        $scope.selectedPage = 1;
        servantService.applyList($scope.search, $scope.limit, $scope.selectedPage, $scope.app_id, function (json) {

            $scope.list = json.list;
            $scope.count = json.count;
            $scope.currentPage = 1;
            $scope.getPage(json.count);
        });
    };
    /*搜索时获取技能、证书、服务项目列表  start*/
    $scope.technique_list = function () {
        servantService.techniqueList($scope.app_id, function (json) {
            $scope.techniquelist = json['list'];
        });
    };
    $scope.technique_list();
    $scope.cer_list = function () {
        servantService.cerList($scope.app_id, function (json) {
            $scope.cerslist = json['list'];
        });
    };
    $scope.cer_list();

    //获取服务类型列表
    $scope.getserviceType1 = function (code) {
        var data = {
            'code': code
        };

        baas_jz.call({
            'http': $http,
            'method': 'post',
            'data': data,
            'uri': 'order/orderList/serviceList',
            'success': function (d) {
                console.log('test==');
                $scope.serviceList = d.list;
                console.log($scope.serviceList);
            }
        });


    }

    $scope.getserviceType1('0');
    $scope.getserviceType2 = function (code) {
        var data = {
            'code': (code != '' && code != undefined) ? code : ''
        };
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'data': data,
            'uri': 'order/orderList/serviceList',
            'success': function (d) {
                $scope.serviceList2 = d.list;
                $scope.search.serviceType1code = code;
                $scope.search.serviceType2code = '';
            }
        });


    }
    //小类
    $scope.getserviceType3 = function (code) {
        var data = {
            'code': (code != '' && code != undefined) ? code : ''
        };
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'data': data,
            'uri': 'order/orderList/serviceList',
            'success': function (d) {
                $scope.serviceList3 = d.list;
            }
        });


    }
    //获得申请企业
    $scope.getCompanyList = function () {
        baas_jz.call({
            'http': $http,
            'method': 'get',
            'uri': 'company/company/list',
            'success': function (d) {
                $scope.companyList = d.list;

            }
        });


    }
    $scope.getCompanyList();
    /*分页*/
    $scope.changePage = function (p) {
        $scope.data.selectedPage = p;
        servantService.applyList($scope.search, $scope.limit, p, $scope.app_id, function (json) {
            $scope.list = json.list;
            $scope.count = json.count;
            $scope.currentPage = p;
            $scope.getPage(json.count);
        });
    };
    $scope.getPage = function (count) {
        $scope.pageList = [];
        var limit = 20; //返回的条数
        $scope.totalPage = parseInt(count / limit); //总页数
        $scope.totalPage += count % limit >= 1 ? 1 : 0; //判断页数是否是整数
        var start = ($scope.currentPage - 2) >= 1 ? $scope.currentPage - 2 : 1; //首页
        var end = ($scope.currentPage + 2) <= $scope.totalPage ? $scope.currentPage + 2 : $scope.totalPage; //尾页

        $scope.frontdotShow = start > 1 ? true : false; //前面的省略号
        $scope.backdotShow = end < $scope.totalPage ? true : false; //后面的省略号
        for (var i = start; i <= end; i++) {
            $scope.pageList.push({
                number: i,
                active: i == $scope.currentPage ? 'active' : ''
            }); //给点击的按钮添加class
        }
    }
    //审核操作
    $scope.editStatus = function (cooperation, status) {
        var data = {
            status: status

        };
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'data': data,
            'uri': 'company/cooperation/edit/' + cooperation.cooperation_id,
            'success': function (d) {
                $scope.getList();
            }
        });


    }


}

function servantAdminListController($scope, $rootScope, $routeParams, $window, $location, $http, servantService, orderService) {
    $scope.app_id = $routeParams.app_id;
    $scope.URL = WEB_URL;
    $scope.search = {};
    $scope.currentPage = 1 ;
    var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));

    $scope.isCompanyAcount = typeof manager.company_code === "undefined" || manager.company_code == null ? false : true;
    console.log($scope.isCompanyAcount)
    if (manager != undefined && manager.isPension) {
        $scope.search.manager_group_id = manager.manager_group_id;
    }
    ////社区业务员
    if (typeof manager != undefined && manager.manager_id != undefined && manager.manager_id != '' && manager.type_code == 'commService') {
        $scope.search.record_manager_id = manager.manager_id;
    }
    $scope.limit = 20;
    $scope.getList = function () {
        servantService.adminList($scope.search,$scope.limit,$scope.currentPage, function (json) {
            $scope.list = json.list;
            $scope.count = json.count;
            $scope.currentPage = 1;
            $scope.getPage(json.count);
        });
    };

    $scope.getList();
    $scope.searchList = function (data) {
        var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
        if (manager != undefined && manager.isPension) {
            data.manager_group_id = manager.manager_group_id;
        }
        ////社区业务员
        if (typeof manager != 'undefined' && manager.manager_id != undefined && manager.manager_id != '' && manager.type_code == 'commService') {
            data.record_manager_id = manager.manager_id;
        }
        console.log($scope.search);
        servantService.adminList($scope.search,$scope.limit,$scope.currentPage, function (json) {
            $scope.list = json.list;
            $scope.count = json.count;
            $scope.currentPage = 1;
            $scope.getPage(json.count);
        });
    };

    /*搜索时获取技能、证书、服务项目列表  start*/
    $scope.technique_list = function () {
        servantService.techniqueList($scope.app_id, function (json) {
            $scope.techniquelist = json['list'];
        });
    };
    $scope.technique_list();
    $scope.cer_list = function () {
        servantService.cerList($scope.app_id, function (json) {
            $scope.cerslist = json['list'];
        });
    };
    $scope.cer_list();

    //服务项目

    /*分页*/
    $scope.changePage = function (p) {
        $scope.selectedPage = p;
        servantService.adminList($scope.search,$scope.limit,$scope.selectedPage = p, function (json) {
            $scope.list = json.list;
            $scope.count = json.count;
            $scope.currentPage = p;
            $scope.getPage(json.count);
        });
    };
    $scope.getPage = function (count) {
        $scope.pageList = [];
        var limit = 20; //返回的条数
        $scope.totalPage = parseInt(count / limit); //总页数
        $scope.totalPage += count % limit >= 1 ? 1 : 0; //判断页数是否是整数
        var start = ($scope.currentPage - 2) >= 1 ? $scope.currentPage - 2 : 1; //首页
        var end = ($scope.currentPage + 2) <= $scope.totalPage ? $scope.currentPage + 2 : $scope.totalPage; //尾页

        $scope.frontdotShow = start > 1 ? true : false; //前面的省略号
        $scope.backdotShow = end < $scope.totalPage ? true : false; //后面的省略号
        for (var i = start; i <= end; i++) {
            $scope.pageList.push({
                number: i,
                active: i == $scope.currentPage ? 'active' : ''
            }); //给点击的按钮添加class
        }
    };
    $scope.delete = function (servant) {
        servantService.delete(servant.servant_id, $scope.app_id, function (json) {
            if (json.status) {
                $scope.getList();
            } else {
                if (typeof json.description) {
                    alert(json.description);
                } else {
                    alert('由于网络原因，请稍后重试');
                }
            }
        });
    };
    $scope.import = function () {
        var path = angular.element('#filepath').val();
        servantService.import(path, $scope.app_id, function (json) {
            if (json.status) {
                alert(json.description);
                closeMod();
                $scope.getList();
            } else
                alert('导入失败，请重新导入');
        });
    };

    $scope.getserviceType1 = function (code) {
        orderService.getServiceListByCode(code, function (d) {
            $scope.serviceList1 = d.list;
            $scope.serviceList2 = [];
            $scope.serviceList3 = [];
            $scope.search.serviceType2 == null;
            $scope.search.serviceType3 == null;
        });
    };
    $scope.getserviceType2 = function (code) {
        orderService.getServiceListByCode(code, function (d) {
            $scope.serviceList2 = d.list;
            $scope.serviceList3 = [];
            $scope.search.serviceType3 = null;
        });
    };
    $scope.getserviceType3 = function (code) {
        orderService.getServiceListByCode(code, function (d) {
            $scope.serviceList3 = d.list;
        });
    };
    $scope.getserviceType1('0');

    //$scope.getserviceType1 = function (code) {
    //
    //    var data = {
    //        'code': (code != '' && code != undefined) ? code : ''
    //    };
    //    baas_jz.call({
    //        'http': $http,
    //        'method': 'post',
    //        'data': data,
    //        'uri': 'order/orderList/serviceList',
    //        'success': function (d) {
    //
    //            $scope.serviceList = d.list;
    //        }
    //    });
    //
    //
    //}
    ////中类
    //$scope.getserviceType2 = function (code) {
    //    var data = {
    //        'code': (code != '' && code != undefined) ? code : ''
    //    };
    //    baas_jz.call({
    //        'http': $http,
    //        'method': 'post',
    //        'data': data,
    //        'uri': 'order/orderList/serviceList',
    //        'success': function (d) {
    //            $scope.serviceList2 = d.list;
    //        }
    //    });
    //
    //
    //}
    ////小类
    //$scope.getserviceType3 = function (code) {
    //    var data = {
    //        'code': (code != '' && code != undefined) ? code : ''
    //    };
    //    baas_jz.call({
    //        'http': $http,
    //        'method': 'post',
    //        'data': data,
    //        'uri': 'order/orderList/serviceList',
    //        'success': function (d) {
    //            $scope.serviceList3 = d.list;
    //        }
    //    });
    //
    //}
    //$scope.getserviceType1('0');

    // 修改后返回 当前页 by gxl 20150928
    $scope.getListNew = function () {
        var currentPage = $scope.currentPage
        servantService.list($scope.search, $scope.limit, $scope.selectedPage, $scope.app_id, function (json) {
            $scope.list = json.list;
            $scope.changePage(currentPage);
        });
    };
    //////////////////////////pension  ----  add  servant////////////////////////////////////
    $scope.checkServant = function (servant_id, verifystatus) {
        var data = {
            verifystatus: verifystatus
        }
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'uri': 'servant/servant/edit/' + servant_id,
            'data': data,
            'success': function (d) {
                if (d.status == '1') {
                    $scope.getListNew();
                } else {

                    if (d.description) {
                        alert(d.description);
                    } else {
                        alert('网络原因，请求失败，请稍后重试');
                    }

                }
            }
        });
    };


    $scope.servantRes = {};
    $scope.searchForPhone = function (data) {
        servantService.searchForPhone(data, function (d) {
            $scope.servantRes = d;
        });
    }
    $scope.saveServant = function (data) {
        var manager = JSON.parse(localStorage.getItem(sessionStorage.getItem("who") + "manager"));
        var params = {
            manager_id: manager.manager_id,
            servant_id: data._id.$id
        }

        servantService.save(params, function (d) {
            alert("添加成功");
            closeMod();

            $scope.getList();
            $scope.clearServant();
        });
    }
    $scope.clearServant = function () {
        $scope.servant = {};
        $scope.servantRes = {};
    }
    $scope.deleteServant = function (obj) {
        var params = {
            manager_id: "0",
            servant_id: obj.servant_id
        }
        baas_jz.call({
            'http': $http,
            'method': 'post',
            'uri': 'servant/servant/servant/' + params.servant_id + "/manager/0",
            'data': params,
            'success': function (d) {
                if (d.status == '1') {
                    $scope.getList();
                } else {
                    if (d.description) {
                        alert(d.description);
                    } else {
                        alert('网络原因，请求失败，请稍后重试');
                    }

                }
            }
        });
    }
}